<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>check log websocket</title>
</head>
<body>
    <xblock>
        <textarea style="width: 100%;height:880px;background-color: black;color: greenyellow; font-size: 13px;" class="form-control" id="chat-log" disabled rows="20"></textarea>
    </xblock>
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script>
            // 实时获取当前在线用户人数
            var uuid_str = "{{ uuid_str }}"     // uuid_str 是服务器传过来的
            protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
            socketURL = protocol + window.location.host + "/look_single_task_log-{{ uuid_str }}/";
            // socketURL = protocol + location.hostname + ":8913" + "/look_single_task_log-{{ uuid_str }}/";
            console.log(socketURL,uuid_str);
            var socket = new WebSocket(socketURL);
            socket.onopen = function () {
                console.log('WebSocket open，成功与服务器建立连接...');//成功连接上Websocket
                socket.send(uuid_str, uuid_str);//发送数据到服务端
            };
            socket.onmessage = function (e) {
                var data = JSON.parse(e.data);
                // console.log(data);
                var message = data['message'];
                document.querySelector('#chat-log').value += (message);
                // 跳转到页面底部
                $('#chat-log').scrollTop($('#chat-log')[0].scrollHeight);
            };
            socket.onclose=function(e){
              console.log(e);
              socket.close(); //关闭TCP连接
              console.log('WebSocket close，服务器判断，主动断开连接...');//关闭Websocket
            };
            if (socket.readyState == WebSocket.OPEN) socket.onopen();
    </script>
</body>
</html>
